import request from '@/utils/request'

// 获取订单列表
export function listOrders(params) {
  return request({
    url: '/api/orders',
    method: 'get',
    params
  })
}

// 获取订单详情
export function getOrder(id) {
  return request({
    url: `/api/orders/${id}`,
    method: 'get'
  })
}

// 创建订单
export function createOrder(data) {
  return request({
    url: '/api/orders',
    method: 'post',
    data
  })
}

// 更新订单
export function updateOrder(id, data) {
  return request({
    url: `/api/orders/${id}`,
    method: 'put',
    data
  })
}

// 删除订单
export function deleteOrder(ids) {
  return request({
    url: `/api/orders/${ids}`,
    method: 'delete'
  })
}

// 取消订单
export function cancelOrder(id) {
  console.log('调用取消订单API, ID:', id)
  if (!id) {
    console.error('取消订单失败: 订单ID不能为空')
    return Promise.reject(new Error('订单ID不能为空'))
  }
  
  return request({
    url: `/api/orders/${id}/cancel`,
    method: 'post'
  }).catch(error => {
    console.error('取消订单API错误:', error)
    throw error
  })
}

// 更新订单状态
export function updateOrderStatus(id, status) {
  return request({
    url: `/api/orders/${id}/status`,
    method: 'put',
    data: { status }
  })
}

// 发货
export function shipOrder(id, data) {
  return request({
    url: `/api/orders/${id}/ship`,
    method: 'put',
    data
  })
}
